package com.zhou.common.operator.log.web.controller;

import com.zhou.common.operator.log.core.ao.OperateLogInfoGetPageListAO;
import com.zhou.common.operator.log.core.model.OperateLogInfo;
import com.zhou.common.operator.log.core.service.OperateLogStorageService;
import com.zhou.common.operator.log.web.req.OperateLogInfoRequestConverter;
import com.zhou.common.operator.log.web.req.OperateLogRecordRequest;
import com.zhou.common.pub.model.CommonPage;
import com.zhou.common.pub.model.Response;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Api(
        tags = {"操作日志接口"}
)
@RequestMapping({"/operateLog"})
public class OperateLogController {
    @Autowired
    OperateLogStorageService operateLogStorageService;

    public OperateLogController() {
    }

    @GetMapping({"/list"})
    @ApiOperation(
            value = "分页查询操作日志",
            notes = "分页查询结果"
    )
    public Response<CommonPage<OperateLogInfo>> list(OperateLogInfoGetPageListAO ao) {
        CommonPage<OperateLogInfo> pageList = this.operateLogStorageService.getPageList(ao);
        return Response.success(pageList);
    }

    @PostMapping({"/append"})
    @ApiOperation(
            value = "追加日志",
            notes = "追加日志记录"
    )
    public Response append(@RequestBody OperateLogRecordRequest newItem) {
        this.operateLogStorageService.append(OperateLogInfoRequestConverter.operateLogRequestConveter(newItem));
        return Response.success();
    }

    @PostMapping({"/batch/append"})
    @ApiOperation(
            value = "追加日志",
            notes = "追加日志记录"
    )
    public Response batchAppend(@RequestBody List<OperateLogRecordRequest> newItems) {
        newItems.forEach((newItem) -> {
            this.operateLogStorageService.append(OperateLogInfoRequestConverter.operateLogRequestConveter(newItem));
        });
        return Response.success();
    }
}

